package com.work.dyduser.module.pagetwo;


import android.content.Context;
import android.content.Intent;
import android.media.MediaRecorder;
import android.os.Environment;
import android.text.TextUtils;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;

import com.work.dyduser.R;
import com.work.dyduser.base.BaseActivity;
import com.work.dyduser.cache.Ckey;
import com.work.dyduser.cache.SPutils;
import com.work.dyduser.utils.CountdownButton;
import com.work.dyduser.utils.Recorderutil;
import com.work.dyduser.utils.UIUtils;
import com.work.dyduser.utils.ViewUtils;

import java.util.Timer;
import java.util.TimerTask;


/**
 * author: Wangzhan
 * date :2018/7/26 0026 17:19
 * version:2.0.0
 * description:拍摄视频
 * <p>
 * 作者：changer0
 * 链接：https://www.jianshu.com/p/6f84739ab85f
 * 來源：简书
 * 简书著作权归作者所有，任何形式的转载都请联系作者获得授权并注明出处。
 */
public class VideoActivity extends BaseActivity {


    private SurfaceView surfaceview;
    private ImageView iv_pai;
    private CountdownButton bt_getcode;
    public Recorderutil recorderutil;
    public static boolean isRuning;
    private MediaRecorder mediaRecorder;
    private Context context;
    private String filepath;
    private String outputDir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    //录制时长
    public static int videotime = 20000;


    @Override
    protected int getLayoutId() {
        return R.layout.activity_paishe_video;
    }

    @Override
    protected void initView() {
        context = this;
        mTitleBar.titleBack.setVisibility(View.VISIBLE);
        if (TextUtils.isEmpty(SPutils.get(Ckey.USERID))) {
            mTitleBar.titleMiddle.setText("拍摄视频");
        } else {
            mTitleBar.titleMiddle.setText(SPutils.get(Ckey.USERID));
        }
        surfaceview = findViewById(R.id.main_surface_view);
        iv_pai = findViewById(R.id.iv_pai);
        bt_getcode = findViewById(R.id.bt_getcode);
        if (TextUtils.isEmpty(SPutils.get(Ckey.USERID))) {
            UIUtils.showToast("请先去扫描二维码,再上传视频");
        }
        ViewUtils.setOnClickListeners(this, iv_pai);
    }

    @Override
    protected void initData() {
        startMyRecord();
        //传入ImageView
        bt_getcode.setImageView(iv_pai);
    }

    @Override
    protected void processClick(View v) {
        super.processClick(v);
        switch (v.getId()) {
            case R.id.iv_pai:
                if (TextUtils.isEmpty(SPutils.get(Ckey.USERID))) {
                    UIUtils.showToast("请先去扫描二维码,再上传视频");
                    return;
                }
                if (isRuning) {
                    //停止摄像
                    iv_pai.setImageResource(R.drawable.shexiang);
                    isRuning = false;
                    recorderutil.stopRecordSave();
                    bt_getcode.clearTimer();
                } else {
                    //开始摄像
                    iv_pai.setImageResource(R.drawable.tingzhi);
                    recorderutil.startRecord();
                    bt_getcode.start();
                    isRuning = true;
                }
                break;
        }
    }

    //开始拍摄预览
    private void startMyRecord() {
        SurfaceHolder holder = surfaceview.getHolder();
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        holder.addCallback(new SurfaceHolder.Callback() {
            public SurfaceHolder surfaceHolder;

            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                surfaceHolder = holder;
                mediaRecorder = new MediaRecorder();
                recorderutil = new Recorderutil(VideoActivity.this, mediaRecorder, surfaceview, videotime);
                recorderutil.setListener(new Recorderutil.onListener() {
                    @Override
                    public void OnListener(String msg) {
                        showLoaddialog();
                        Timer timer = new Timer();
                        timer.schedule(new TimerTask() {
                            @Override
                            public void run() {
                                runOnUiThread(new Runnable() {
                                    @Override
                                    public void run() {
                                        hideLoaddialog();
                                        //计时器设置为0
                                        bt_getcode.setLength(0);
                                        bt_getcode.setText("00:00");
                                        filepath = Recorderutil.filePath;
                                        Intent intent = new Intent(context, PlayVideoActivity.class);
                                        intent.putExtra("videoPath", filepath);
                                        UIUtils.startActivity(intent);
                                        finish();
//                                        //拍摄完成显示弹框
//                                        showInputPopup();
                                    }
                                });
                            }
                        }, 1000);

                    }
                });
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {


            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
            }
        });
    }


}